<?php
	/**
	* Perform search 
	* @author: trungdt
	* @since: Oct 25, 2011
	* @version: 1.0
	* 
	*  
	* Change logs
	*/
	if (!defined('EXPONENT')) exit('');
	$config=$db->selectObject("productsmodule_config", "location_data = '".serialize($loc)."'");
	if (!$config)
	{
		header("Location: /");
		die();
	}
	
	// for the condition
	$lang=exponent_lang_loadFile("modules/productsmodule/common.php");
	
	// perform the search
	$search_string="";
	if (isset($_REQUEST['search_string']))
		$search_string = _ab_safe_html_string(strip_tags($_REQUEST['search_string']));
	$bk_search_string=$search_string;
	$search_string=explode(" ", $search_string);
	if (count($search_string)  == 0)
	{
		header("Location: /");
		die();
	}
	
	// get cat
	$cat=null;
	if (isset($_REQUEST['cat_name']))
	{
		$cat_seo_name=_ab_safe_html_string($_REQUEST['cat_name']);
		$cat=productsmodule::__GetCategoryBySeoName($cat_seo_name);
	}
	else
	{
		$cat->seo_name = "";
		$cat->title= $lang['search_result_of']."`".$bk_search_string."`";
		$cat->id=0;
	}
	// get sub cats
	$sub_cats=productsmodule::__GetCategoryByParentID($cat->id);
	// we force to change link of each cat, to control the direction
	foreach ($sub_cats as $k=>$v)
	{
		$v->link=exponent_core_makeLink(array(
			'module'=>'productsmodule',
			'action'=>'search',
			'src'=>$loc->src,
			'search_string'=>$bk_search_string,
			'cat_name'=>$v->seo_name,
		));
	}
	
	// product state
	$product_state=array();
	for ($i=0;$i<$lang['product_state_count'];$i++)
	{
		$product_state[$i]=$lang["product_state{$i}"];
	}

	// product state2
	$product_state2=array();
	for ($i=0;$i<$lang['product_state_2_count'];$i++)
	{
		$product_state2[$i]=$lang["product_state_2_{$i}"];
	}

	// sort by
	$sort_by=array();
	for ($i=0;$i<$lang['pv_sort_by_count'];$i++)
	{
		$sort_by[$i]=$lang["pv_sort_by{$i}"];
	}

	exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_ACTION);

	// fetch to template
	$template=new template("productsmodule","_CategoriesViewProduct",$loc);
	$template->register_permissions(array('administrate',"configure", "create", "edit", "delete"),$loc);
	// fake, dont show the top and bottom container
	$template->assign("src_top", "");
	$template->assign("src_bottom", "");
	
	$template->assign("config", $config);
	
	$beadcum=array();
	if ($cat->id != 0)
	{
		$beadcum=productsmodule::__PDoremiShop_CategorySeoNameBuild($cat->id, 1);
		foreach ($beadcum as $k=>$v)
		{
			$v->link=exponent_core_makeLink(array(
				'module'=>'productsmodule',
				'action'=>'search',
				'src'=>$loc->src,
				'search_string'=>$bk_search_string,
				'cat_name'=>$v->seo_name,
		));
	}
	}
		
	$temp=null;
	$temp->link=exponent_core_makeLink(array(
		"module"=>'productsmodule',
		"action"=>'search',
		"search_string"=>$bk_search_string,
		"src"=>$loc->src
	));
	$temp->title=$lang['search_result_of']."`".$bk_search_string."`";
	$beadcum[]=clone $temp;
	
	$template->assign("breadcum", $beadcum);

	// product state
	$template->assign("product_states", $product_state);
	$template->assign("product_states2", $product_state2);
	$template->assign("sort_by", $sort_by);
	
	// pricelist
	$currency=productsmodule::_GetCurrencyDefault($config->currency_id);
	$price_list=productsmodule::__Generate_Price_Filter_list($currency);
	
	// for product list
	$template->assign("brands", productsmodule::__GetBrandList());
	$template->assign("selected_cat", $cat);
	$template->assign("sub_cats", $sub_cats);
	$template->assign("price_list", $price_list);
	
	$config_preprocess="__abpv_config.action_mode=1; __abpv_config.search_string='{$bk_search_string}';";
	if ($cat->id != 0)
		$config_preprocess.="__abpv_config.cat_seo_name = '{$cat->seo_name}';";
	$template->assign("config_preprocess", $config_preprocess);
	
	// for chaining to product_getajax
	$_REQUEST['action_mode']=1;
	$_REQUEST['search_string']=$bk_search_string;
	if ($cat->id >0)
		$_REQUEST['cat_seo_name']=$cat->seo_name;
	// product list will be chained in template

	// set page title
	_ab_theme_action_register_title($lang['search_result_of']."`".$bk_search_string."`");
	_ab_theme_action_register_title("",1,0);


	$template->output();
?>
